#include "state_intro.h"
#include "krryn.h"

using namespace krryn::math;
using namespace krryn::system;
using namespace krryn::usr;
using namespace krryn::core;
using namespace krryn::scene;
using namespace krryn::view;

namespace krryn{
namespace usr{
	class audio_thread : public thread{
	public:
		virtual void run(){
			using namespace krryn::audio;

			di l_DI;

			mixer::ptr l_Mixer = l_DI.create_mixer();
			input::ptr l_Sound = l_DI.create_input("./audio/sr10_Aesthetic Perfection - The Ones.wav");
			l_Mixer->play(l_Sound);

			while(true){
				l_Mixer->update();
			}
		}
	};
}
}

static state_intro _state_intro;

state_intro::~state_intro(){
	delete m_Driver;
	delete m_Audio;
	delete m_Camera;
	delete m_Controller;
	delete m_Mesh;

	// delete m_Renderer;

	// deleting m_Manager sometimes deadlocks?
	// delete m_Manager;
}

void state_intro::init(){
	// generic stuff that you always have to do
	system_info i;

	m_Driver	 = driver::create("opengl");
	m_Manager	 = new job_manager(i.number_of_processors());
	m_Renderer	 = m_Driver->create_renderer(m_Manager);
	m_Camera	 = new camera_fps();
	m_Controller = new camera_fps_controller_default(
		m_Camera, 
		m_Renderer->get_input_manager()
	);

	m_Renderer->set_camera(m_Camera);

	// needs refactoring
	m_Audio		 = new audio_thread();
	m_Audio->start();

	// specific to scene
	mesh_procedural mpl;
	m_Mesh		 = mpl.create_sphere(1 * vector3f(1, 1, 1), 5);
}

void state_intro::activate(){}

void state_intro::deactivate(){}

void state_intro::tick(float a_DeltaTime){
//	m_Controller->update(a_DeltaTime);

	m_Renderer->begin_scene();

	//matrix l_Base;

	//static float s = 0.01;
	//static float z = 0;
	//static float q = 100;

	//l_Base.rotate_y(q);
	//l_Base.scale(s);
	//l_Base.translate(vector3f(0, z, z));

	//vector3f v0(0.1, 0, 0), v1(0, 0, 0);
	//float h = 0.01;
	//float a = 10.0;
	//float b = 28.0;
	//float c = 8.0 / 3.0;

	//static int f = 400;

	//for(int i = 0; i < f; i++){
	//	v1.m_X = v0.m_X + h * a * (v0.m_Y - v0.m_X);
	//	v1.m_Y = v0.m_Y + h * (v0.m_X * (b - v0.m_Z) - v0.m_Y);
	//	v1.m_Z = v0.m_Z + h * (v0.m_X * v0.m_Y - c * v0.m_Z);
	//	v0 = v1;
	//	if(i > 100){
	//		matrix l_ToWorld;
	//		l_ToWorld.translate(v0);
	//		l_ToWorld.concatenate(l_Base);

	//		m_Renderer->draw(m_Mesh, l_ToWorld);
	//	}
	//}

	m_Renderer->end_scene();
}
